package JDBCConnect;

import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-09-04
 * Time: 11:23
 */

public class JDBC {
    public static void main(String[] args){
        //加载驱动类
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动加载成功");

            //构建连接参数
            String URL = "jdbc:mysql://localhost:3306/date";
            String USER = "root";
            String PASSWORD = "root";

            //获取连接对象
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println("数据库连接成功");

            //获取执行SQL语句的对象
            Statement stat = conn.createStatement();
            ResultSet result = stat.executeQuery("select * from user");
            while(result.next()){
                System.out.print(result.getInt("id") + " ");
                System.out.print(result.getString("name") + " ");
                System.out.print(result.getString("gender") + " ");
                System.out.println();
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("驱动加载失败");
        }
    }
}
